package main;

import kdd.BiasManager;
import kdd.DataTransfer;
import kdd.DbType;
import kdd.ItemRating;
import kdd.MetaData;
import util.DataReader;
import util.ExecutionTimer;

public class Main {
	private final static String TRACK1_TRAINING_FILE  =  "trainIdx1.txt";
	private final static String TRACK1_VALIDATION_FILE = "validationIdx1.txt";
	private final static String TRACK1_TEST_FILE = "testIdx1.txt";
//	private final static String TRACK1_TRAINING_FILE  =  "E:\\kddcup_data\\track1\\trainIdx1.txt";
//	private final static String TRACK1_VALIDATION_FILE = "E:\\kddcup_data\\track1\\validationIdx1.txt";
//	private final static String TRACK1_TEST_FILE = "E:\\kddcup_data\\track1\\testIdx1.txt";
	private final static String TRACK1_STATS_FILE = "stats1.txt";
	private final static String TRACK_1_RESULTS_FILE = "track1_results_java.txt";

	public static void main(String[] args) {

		double trainingRmse=0, validationRmse=0;
		int iterCount=-1;

		//Gradient Descent:
		int iterations = 20;

		DataReader dr = new DataReader();
		BiasManager biasManager = new BiasManager();
		ExecutionTimer t = new ExecutionTimer();
		ExecutionTimer total = new ExecutionTimer();
		total.start();
	    t.start();

	    MetaData trainingMetaData = dr.readStats(TRACK1_STATS_FILE);

	    MetaData validationMetaData = new MetaData();
	    MetaData testMetaData = new MetaData();

	    validationMetaData.setTrainingTotalItems(trainingMetaData.getTrainingTotalItems());
	    validationMetaData.setTrainingTotalRatings(trainingMetaData.getTrainingTotalRatings());
	    validationMetaData.setTrainingTotalUsers(trainingMetaData.getTrainingTotalUsers());
	    validationMetaData.setValidationTotalRatings(trainingMetaData.getValidationTotalRatings());

	    testMetaData.setTrainingTotalItems(trainingMetaData.getTrainingTotalItems());
	    testMetaData.setTrainingTotalRatings(trainingMetaData.getTrainingTotalRatings());
	    testMetaData.setTrainingTotalUsers(trainingMetaData.getTrainingTotalUsers());
	    testMetaData.setTestTotalRatings(trainingMetaData.getTestTotalRatings());

	    biasManager.setTrainingMetaData(trainingMetaData);
	    biasManager.setValidationMetaData(validationMetaData);
	    biasManager.allocate();
	    t.endAndPrintln();

	    System.out.println("-----------------  LOADING TRAINING DATA -----------------");
	    t.start();
	    DataTransfer trainingData = dr.readTrack1DBFromTextFiles(TRACK1_TRAINING_FILE, DbType.TRAINING, trainingMetaData);
	    ItemRating[] trainingItemRating = trainingData.getItemRatings();
	    t.endAndPrintln();

	    System.out.println("-----------------  LOADING VALIDATION DATA -----------------");
	    t.start();
	    DataTransfer validationData = dr.readTrack1DBFromTextFiles(TRACK1_VALIDATION_FILE, DbType.VALIDATION, validationMetaData);
	    ItemRating[] validationItemRating = validationData.getItemRatings();
	    t.endAndPrintln();

	    System.out.println("-----------------  LOADING TEST DATA -----------------");
	    t.start();
	    DataTransfer testData = dr.readTrack1DBFromTextFiles(TRACK1_TEST_FILE, DbType.TEST, testMetaData);
	    ItemRating[] testItemRating = testData.getItemRatings();
	    t.endAndPrintln();

	    System.out.println("---------");
	    t.start();
	    biasManager.gradientDescent(iterations,trainingRmse,validationRmse,iterCount,trainingItemRating,validationItemRating,trainingData.getPUsersData());
	    System.out.println("---------");
	    t.endAndPrintln();

	    t.start();
	    biasManager.predictTrack1TestRatings(TRACK_1_RESULTS_FILE, testItemRating, testMetaData);
	    t.endAndPrintln();

	    System.out.println("\nTOTAL TIME");
	    total.endAndPrintln();
	}
}
